﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Web
{
    public partial class Console : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["ProjectID"] == null)
                Response.Redirect("~/Index.aspx");
        }

        protected void BtnRun_Click(object sender, EventArgs e)
        {
            int projectID = Convert.ToInt32(Session["ProjectID"]);
            StringBuilder builder = new StringBuilder();
            builder.Append(String.Format("method={0}", DropDownListAPI.SelectedItem.Text));
            builder.Append('&');
            builder.Append("timestamp=343234");
            builder.Append("&");
            builder.Append("sig=sig");

            for (int i = 0; i < GridViewParameter.Rows.Count; i++)
            {
                if (i != GridViewParameter.Rows.Count)
                    builder.Append('&');
                TableRow row = GridViewParameter.Rows[i];
                builder.Append(row.Cells[4].Text);
                builder.Append('=');
                builder.Append(((TextBox)row.Cells[5].FindControl("TxtValue")).Text);
            }

            string parameter = builder.ToString();
            string responseData = null;
            Stopwatch sw = new Stopwatch();
            sw.Start();
            if (RadioButtonPost.Checked)
                responseData = Utility.PostWebRequest(OnlineAPIDao.GetURL(projectID), parameter, Encoding.UTF8);
            else
                responseData = Utility.GetWebRequest(String.Format("{0}?{1}", OnlineAPIDao.GetURL(projectID), parameter));
            sw.Stop();
            //DivResult.InnerHtml = String.Format("花费<b>{0}</b>毫秒<BR>{1}",sw.ElapsedMilliseconds,responseData);
            DivResult.InnerHtml = String.Format("{0}", responseData);
        }

        protected void BtnExit_Click(object sender, EventArgs e)
        {
            Session["ProjectID"] = null;
            Session["ProjectName"] = null;

            Response.Redirect("~/Index.aspx");
        }

        protected void BtnClear_Click(object sender, EventArgs e)
        {
            DivResult.InnerText = "";
        }
    }
}